package com.wl.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wl.enums.BrandStatusEnum;
import com.wl.entity.Brand;
import com.wl.mapper.BrandMapper;
import com.wl.service.BrandService;
import com.wl.utils.JdbcTemplateUtils;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private JdbcTemplateUtils jdbcTemplateUtils;

    @Override
    public Page<Brand> page(Page<Brand> page, BrandStatusEnum status) {
        Page<Brand> brandPage = lambdaQuery().eq(Brand::getStatus, status).page(page);
        return brandPage;
    }

    @SneakyThrows
    @Override
    public void copy() {
//        List<Brand> brands = jdbcTemplate.queryForList(" select * from brand ", Brand.class);
        List<Brand> brands = lambdaQuery().list();
        System.out.println("返回集合: " + objectMapper.writeValueAsString(brands));
        jdbcTemplateUtils.batchInsert(brands,getEntityClass());
    }

    @Override
    public void saveBrand() {
        Brand brand = lambdaQuery().list().get(0);
        save(brand);
    }


}
